﻿

using System;
using System.Linq;
using System.Text;
using ZDK.Infra.Constants;
using ZDK.Infra.Helpers;
using ZDK.Models.Packages;

namespace ZDK.Utilities
{
    public static class PktUtils
    {
        /* 帧序列 SEQ在调试时可简单取 240（需要确认）、224（不需要确认）
         */
        public static byte[] MakeSendPkg(BasePkt pkt, byte[] payload = null)
        {
            byte[] result = null;

            switch(pkt.Afn)
            {
                case Comm.Afn00:
                case Comm.Afn01:
                case Comm.Afn04:
                case Comm.Afn05:
                case Comm.Afn09:
                case Comm.Afn0A:
                case Comm.Afn0C:
                    {
                        var st = new ST_Afn00();
                        result = st.MakeAllFrame(pkt, payload);
                        break;
                    }
                case Comm.Afn02: break;
                case 0x0D: break;
                case 0x0E: break;
                case 0x10: break;
            }
            return result;
        }

        public static string TranslatePkg(byte[] r, ref BasePkt outPkt)
        {
            string s = "";
            string sErr = "报文解析错误！";

            //解析报文
            byte afn = r[(int)FramePosEnum.User_Afn];
            string t = DateTime.Now.ToString("HH:mm:ss");

            switch (afn)
            {
                case Comm.Afn00:
                case Comm.Afn04:
                case Comm.Afn05:
                    {
                        outPkt = new ST_Afn00();
                        var ret = (ST_Afn00)outPkt.ParseAllFrame(r);
                        if (ret == null) return sErr;
                        switch (ret.Fn)
                        {
                            case 1:
                                {
                                    s = $"时间：{ret.TimeStr}, 全部确认";
                                    break;
                                }
                            case 2:
                                {
                                    string err = "无效错误类型";
                                    switch (ret.Err)
                                    {
                                        case 0x01: err = "操作不支持"; break;
                                        case 0x02: err = "非法参数"; break;
                                        case 0x03: err = "校验失败"; break;
                                        case 0xFF: err = "未定错误"; break;
                                    }
                                    s = $"时间：{ret.TimeStr}, 全部否认\r\n错误类型：{err}";
                                    break;
                                }
                            case 3:
                                {
                                    string err = "";
                                    foreach (var e in ret.Items)
                                    {
                                        err += $"Pn={e.Pn},Fn={e.Fn},ERR={(e.Err == 0x0 ? "正确" : "错误")}\r\n";
                                    }
                                    s = $"时间：{ret.TimeStr}, 按数据单元确认和否认\r\n{err}";
                                    break;
                                }
                            case 4:
                                {
                                    s = $"时间：{ret.TimeStr}, 安全认证错误应答\r\n错误类型：{(ret.SecureErr == 0x1 ? "签名校验" : "密文校验")}\r\n数据体：{ret.Data}";
                                    break;
                                }
                        }
                        break; 
                    }
                case Comm.Afn01: 
                    {
                        s = "下行报文，暂不解析";
                        break;
                    }
                case Comm.Afn02:
                    {
                        outPkt = new ST_Afn02();
                        var ret = (ST_Afn02)outPkt.ParseAllFrame(r);
                        if (ret == null) return sErr;
                        switch (ret.Fn)
                        {
                            case 1: s = $"登录时间：{t}"; break;
                            case 2: s = $"退出登录时间：{t}"; break;
                            case 3: s = $"心跳时间：{t}，通信质量：{ret.CommuQuality}"; break;
                        }
                        break;
                    }
                case Comm.Afn0A:
                    {
                        outPkt = new ST_Afn0A();
                        var ret = (ST_Afn0A)outPkt.ParseAllFrame(r);
                        if (ret == null) return sErr;
                        switch (ret.Fn)
                        {
                            case 1:
                                {
                                    s = $"超时时间：{ret.F1_TimeOut}秒\r\n重发次数：{ret.F1_RepeatTime}\r\n需要主动确认(con=1)：{ret.F1_Con}\r\n心跳周期：{ret.F1_HeartTime}分钟\r\n时间：{ret.TimeStr}";
                                    break;
                                }
                            case 2:
                                {
                                    s = $"主IP：{ret.F2_PrimaryIp}:{ret.F2_PrimaryPort}\r\nAPN：{ret.F2_AFN}\r\n用户名：{ret.F2_Username}\r\n密码：{ret.F2_Pwd}\r\n域名1：{ret.F2_Realmname}\r\n副IP：{ret.F2_SecondaryIp}:{ret.F2_SecondaryPort}\r\n域名2：{ret.F2_Realmname2}\r\n时间：{ret.TimeStr}";
                                    break;
                                }
                            case 3:
                                {
                                    s = $"事件记录有效标志位：{ret.F3_Effect}\r\n事件重要性等级标志位：{ret.F3_Important}\r\n事件主动上传标志位：{ret.F3_Initiative}\r\n时间：{ret.TimeStr}";
                                    break;
                                }
                            case 73:
                                {
                                    s = $"进线路数：{ret.F73_Inlines}\r\n温度监测点数：{ret.F73_Temppoints}\r\n用户舱数：{ret.F73_Usercages}\r\n用户舱箱门电子锁：{ret.F73_UserLock}\r\n公共舱箱门电子锁：{ret.F73_PublicLock}\r\n用户舱后门电子锁：{ret.F73_BackLock}\r\n时间：{ret.TimeStr}";
                                    break;
                                }
                            case 74:
                                {
                                    s = $"温度上限值：{ret.F74_Templim}℃\r\n" +
                                            $"温度越上限恢复限值：{ret.F74_Temprecover}℃\r\n" +
                                            $"湿度上限值：{ret.F74_Wetlim}%\r\n" +
                                            $"湿度越上限恢复限值：{ret.F74_Wetrecover}%\r\n" +
                                            $"AC220V电压上限值：{ret.F74_AC220limon}V\r\n" +
                                            $"AC220V电压越上限恢复限值 ：{ret.F74_AC220recoveron}V\r\n" +
                                            $"AC220V 电压下限值：{ret.F74_AC220limoff}V\r\n" +
                                            $"AC220V 电压越下限恢复限值：{ret.F74_AC220recoveroff}V\r\n" +
                                            $"AC24V 电压上限值：{ret.F74_AC24limon}V\r\n" +
                                            $"AC24V 电压越上限恢复限值：{ret.F74_AC24recoveron}V\r\n" +
                                            $"AC24V 电压下限值：{ret.F74_AC24limoff}V\r\n" +
                                            $"AC24V 电压越下限恢复限值 ：{ret.F74_AC24recoveroff}V\r\n" +
                                            $"DC12V 电压上限值：{ret.F74_DC24limon}V\r\n" +
                                            $"DC12V 电压越上限恢复限值：{ret.F74_DC24recoveron}V\r\n" +
                                            $"DC12V 电压下限值：{ret.F74_DC24limoff}V\r\n" +
                                            $"DC12V 电压越下限恢复限值：{ret.F74_DC24recoveroff}V\r\n" +
                                            $"用户舱总功率上限值 ：{ret.F74_Usercagelim}W\r\n" +
                                            $"用户舱总功率上限恢复限值 ：{ret.F74_Usercagerecover}W\r\n" +
                                            $"风扇功率异常系数：{ret.F74_Fanerror}%\r\n" +
                                            $"越限持续时间：{ret.F74_Overloadtime}分钟\r\n时间：{ret.TimeStr}";
                                    break;
                                }
                            case 75:
                                {
                                    s = $"风扇启动温度阈值：{ret.F75_Fanopentemp}℃\r\n风扇关闭温度阈值：{ret.F75_Fanclosetemp}℃\r\n持续时间：{ret.F75_Lasttime}分钟\r\n时间：{ret.TimeStr}";
                                    break;
                                }
                            case 76:
                                {
                                    switch (ret.Pn)
                                    {
                                        case 1: s = "用户舱箱门\r\n"; break;
                                        case 2: s = "公共舱箱门\r\n"; break;
                                        case 3: s = "用户舱后门\r\n"; break;
                                    }
                                    s += $"电子锁设备号：{ret.F76_Eleclockdevice}\r\n电子锁标识号：{ret.F76_EleclockID}\r\n自动闭锁延时时间：{ret.F76_Atuoclose}秒\r\n声光报警持续时间：{ret.F76_Alarmtime}秒\r\n时间：{ret.TimeStr}";
                                    break;
                                }
                            default:
                                break;
                        }
                        break;
                    }
                case Comm.Afn09:
                    {
                        outPkt = new ST_Afn09();
                        var ret = (ST_Afn09)outPkt.ParseAllFrame(r);
                        if (ret == null) return sErr;
                        int pos = 24;
                        switch (ret.Fn)
                        {
                            case 1:
                                {
                                    //厂商代号 4
                                    int len = 4;
                                    string s1 = Encoding.ASCII.GetString(r.Skip(pos).Take(len).ToArray()); pos += len; len = 8;
                                    //厂商设备编号 8
                                    string s2 = Encoding.ASCII.GetString(r.Skip(pos).Take(len).ToArray()); pos += len; len = 8;
                                    //终端软件版本号 8
                                    string s3 = Encoding.ASCII.GetString(r.Skip(pos).Take(len).ToArray()); pos += len; len = 3;
                                    //软件发布日期 3
                                    string s4 = $"{r[pos + 2].ToString("x2")}-{r[pos + 1].ToString("x2")}-{r[pos].ToString("x2")}";
                                    pos += len; len = 11;
                                    //终端配置容量信息码 11
                                    string s5 = Encoding.ASCII.GetString(r.Skip(pos).Take(len).ToArray()); pos += len; len = 8;
                                    //终端通信协议版本号 8
                                    string s6 = Encoding.ASCII.GetString(r.Skip(pos).Take(len).ToArray()); pos += len; len = 8;
                                    //终端硬件版本号 8
                                    string s7 = Encoding.ASCII.GetString(r.Skip(pos).Take(len).ToArray()); pos += len; len = 8;
                                    //终端型号 8
                                    string s8 = Encoding.ASCII.GetString(r.Skip(pos).Take(len).ToArray()); pos += len; len = 3;
                                    //硬件发布日期 3
                                    string s9 = $"{r[pos + 2].ToString("x2")}-{r[pos + 1].ToString("x2")}-{r[pos].ToString("x2")}";
                                    pos += len; len = 1;
                                    //是否有中间继电器
                                    string s10 = r[pos] == 0x0 ? "否" : "是"; pos += len; len = 1;
                                    //终端设备类型 0f
                                    string s11 = r[pos] == 0x0f ? "综合设备箱监控管理单元" : $"{r[pos].ToString("x")}"; pos += len; len = 1;
                                    //终端设备支持功能模块数
                                    string s12 = r[pos].ToString(); pos += len; len = 20;
                                    //ICCID 20
                                    string s13 = Encoding.ASCII.GetString(r.Skip(pos).Take(len).ToArray()); pos += len; len = 1;
                                    //支持独立控制灯数 1
                                    string s14 = r[pos].ToString(); pos += len; len = 3;
                                    //终端出厂日期 3
                                    string s15 = $"{r[pos + 2].ToString("x2")}-{r[pos + 1].ToString("x2")}-{r[pos].ToString("x2")}"; pos += len;

                                    string tt = $"{r[pos + 4].ToString("x2")}年{r[pos + 3].ToString("x2")}:{r[pos + 2].ToString("x2")}:{r[pos + 1].ToString("x2")}";
                                    s = $"{tt}\r\n厂商代号：{s1}\r\n厂商设备编号：{s2}\r\n终端软件版本号：{s3}\r\n软件发布日期：{s4}\r\n终端配置容量信息码：{s5}\r\n终端通信协议版本号：" +
                                        $"{s6}\r\n终端硬件版本号：{s7}\r\n终端型号：{s8}\r\n硬件发布日期：{s9}\r\n是否有中间继电器：{s10}\r\n终端设备类型：{s11}" +
                                        $"\r\n终端设备支持功能模块数：{s12}\r\nICCID：{s13}\r\n支持独立控制灯数：{s14}\r\n终端出厂日期：{s15}";
                                    break;
                                }
                            case 9:
                                {
                                    //电子锁数量 1
                                    int num = r[pos++];
                                    s = $"\r\n电子锁数量：{num}";
                                    //电子锁信息
                                    string s1, s2, s3, s4, s5, s6;
                                    int len = 1;
                                    for (int i = 0; i < num; ++i)
                                    {
                                        s1 = r[pos].ToString("x2"); pos += len; len = 4;
                                        s2 = $"{r[pos + 3].ToString("x2")}{r[pos + 2].ToString("x2")}{r[pos + 1].ToString("x2")}{r[pos].ToString("x2")}"; pos += len; len = 4;
                                        s3 = $"{r[pos + 3].ToString("x2")}{r[pos + 2].ToString("x2")}{r[pos + 1].ToString("x2")}{r[pos].ToString("x2")}"; pos += len; len = 8;
                                        s4 = Encoding.ASCII.GetString(r.Skip(pos).Take(len).ToArray()); pos += len; len = 4;
                                        s5 = $"{r[pos + 3].ToString("x2")}{r[pos + 2].ToString("x2")}{r[pos + 1].ToString("x2")}{r[pos].ToString("x2")}"; pos += len; len = 4;
                                        s6 = $"{r[pos + 3].ToString("x2")}{r[pos + 2].ToString("x2")}{r[pos + 1].ToString("x2")}{r[pos].ToString("x2")}"; pos += len; len = 1;

                                        s += $"\r\n====>箱门号：{s1}\r\n设备号PA：{s2}\r\n标识号ID：{s3}\r\n厂家名称：{s4}\r\n硬件版本：{s5}\r\n软件版本：{s6}";
                                    }
                                    string tt = $"{r[pos + 4].ToString("x2")}年{r[pos + 3].ToString("x2")}:{r[pos + 2].ToString("x2")}:{r[pos + 1].ToString("x2")}";
                                    s = tt + s;
                                    break;
                                }
                            case 80:
                                {
                                    s += $"自定义参数略，时间：{ret.TimeStr}";
                                    break;
                                }
                            default:
                                break;
                        }
                        break;
                    }
                case Comm.Afn0C:
                    {
                        outPkt = new ST_Afn0C();
                        var ret = (ST_Afn0C)outPkt.ParseAllFrame(r);
                        if (ret == null) return sErr;
                        int pos = 24;
                        switch (ret.Fn)
                        {
                            case 2:
                                {
                                    pos = 29;
                                    byte month = (byte)(r[pos - 1] & 0x1F);
                                    s = $"20{r[pos].ToString("x2")}-{month.ToString("x2")}-{r[pos - 2].ToString("x2")} {r[pos - 3].ToString("x2")}:{r[pos - 4].ToString("x2")}:{r[pos - 5].ToString("x2")}";
                                    break;
                                }
                            case 73:
                            case 74:
                                {
                                    var val = FmtHelper.GetAppendB29(r, pos); pos += 3;
                                    string tt = $"{r[pos + 4].ToString("x2")}年{r[pos + 3].ToString("x2")}:{r[pos + 2].ToString("x2")}:{r[pos + 1].ToString("x2")}";
                                    s = $"{tt}，{val}"; break;
                                }
                            case 75:
                                {
                                    pos = 26;
                                    string leak = r[pos - 2] == 1 ? "积水" : "未积水";
                                    string fan = r[pos] == 1 ? "运转" : "未运转";
                                    string userFDoor = (r[pos - 1] & 0x1) == 0 ? "闭合" : "开启";
                                    string commDoor = (r[pos - 1] & 0x2) == 0 ? "闭合" : "开启";
                                    string userBDoor = (r[pos - 1] & 0x4) == 0 ? "闭合" : "开启";
                                    pos++;
                                    string tt = $"{r[pos + 4].ToString("x2")}年{r[pos + 3].ToString("x2")}:{r[pos + 2].ToString("x2")}:{r[pos + 1].ToString("x2")}";
                                    s = $"{tt}，\r\n积水状态：{leak}\r\n风扇状态：{fan}\r\n用户舱箱门：{userFDoor}\r\n公共舱箱门：{commDoor}\r\n用户舱后门：{userBDoor}";
                                    break;
                                }
                            case 76:
                                {
                                    string inline1 = (r[pos] & 0x1) == 1 ? "跳闸" : "合闸";
                                    string inline2 = (r[pos] & 0x2) == 0x2 ? "跳闸" : "合闸";
                                    s = $"\r\n进线1：{inline1}\r\n进线2：{inline2}";
                                    int cabinNum = r[++pos];
                                    pos++;
                                    var cabinStatus = new string[3];
                                    for (int i = 0; i < cabinNum; ++i)
                                    {
                                        cabinStatus[0] = (r[pos + i] & 0x1) == 1 ? "跳闸" : "合闸";
                                        cabinStatus[1] = (r[pos + i] & 0x2) == 0x2 ? "跳闸" : "合闸";
                                        cabinStatus[2] = (r[pos + i] & 0x4) == 0x4 ? "跳闸" : "合闸";
                                        s += $"\r\n用户舱{i + 1}（AC220V）：{cabinStatus[0]}\r\n用户舱{i + 1}（AC24V）：{cabinStatus[1]}\r\n用户舱{i + 1}（DC12V）：{cabinStatus[2]}";
                                    }
                                    pos += cabinNum;
                                    string tt = $"{r[pos + 4].ToString("x2")}年{r[pos + 3].ToString("x2")}:{r[pos + 2].ToString("x2")}:{r[pos + 1].ToString("x2")}";
                                    s = $"{tt}，" + s;
                                    break;
                                }
                            case 77:
                                {
                                    s = $"重要事件：{ret.F77_EC1}\r\n一般事件：{ret.F77_EC2}\r\n时间：{ret.TimeStr}";
                                    break;
                                }
                            case 78:
                                {
                                    s = $"电子锁锁舌状态:{(ret.F78_ElecLock == 0 ? "打开" : (ret.F78_ElecLock == 1 ? "关闭" : "未知"))}\r\n时间:{ret.TimeStr}";
                                    break;
                                }
                            case 81:
                                {
                                    s = $"AC220V电压：{ret.F81_AC220V}V\r\nAC24V电压：{ret.F81_AC24V}V\r\nDC12电压：{ret.F81_DC12V}V\r\nAC220V电流：{ret.F81_AC220A}A\r\nAC24V电流：{ret.F81_AC24A}A\r\nDC12V电流：{ret.F81_DC12A}A\r\n时间：{ret.TimeStr}";
                                    break;
                                }
                            case 82:
                                {
                                    s = $"AC220V功率：{ret.F82_AC220W}W\r\nAC24V功率：{ret.F82_AC24W}W\r\nDC12功率：{ret.F82_DC12W}W\r\n总功率：{ret.F82_Power}W\r\n时间：{ret.TimeStr}";
                                    break;
                                }
                            case 83:
                                {
                                    s = $"AC220V当日电量：{ret.F83_AC220kWh}kWh\r\nAC24V当日电量：{ret.F83_AC24kWh}kWh\r\nDC12当日电量：{ret.F83_DC12kWh}kWh\r\n当日总电量：{ret.F83_AllkWh}kWh\r\n时间：{ret.TimeStr}";
                                    break;
                                }
                            case 84:
                                {
                                    s = $"AC220V当日电量：{ret.F84_AC220kWh}kWh\r\nAC24V当日电量：{ret.F84_AC24kWh}kWh\r\nDC12当日电量：{ret.F84_DC12kWh}kWh\r\n当日总电量：{ret.F84_AllkWh}kWh\r\n时间：{ret.TimeStr}";
                                    break;
                                }
                            case 85:
                                {
                                    s = $"进线电压：{ret.F85_InV}V\r\n进线电流：{ret.F85_InA}A\r\n时间：{ret.TimeStr}";
                                    break;
                                }
                            case 86:
                                {
                                    s = $"进线功率：{ret.F86_InW}W\r\n时间：{ret.TimeStr}";
                                    break;
                                }
                            case 87:
                                {
                                    s = $"进线当日电量：{ret.F87_InDaykWh}kWh\r\n时间：{ret.TimeStr}";
                                    break;
                                }
                            case 88:
                                {
                                    s = $"进线当月电量：{ret.F88_InMonthkWh}kWh\r\n时间：{ret.TimeStr}";
                                    break;
                                }
                            case 89:
                                {
                                    s = $"风扇功率：{ret.F89_FanW}W\r\n时间：{ret.TimeStr}";
                                    break;
                                }
                            default:
                                break;
                        }
                        break;
                    }
                case Comm.Afn0D:
                    {
                        //switch (Fn)
                        //{
                        //    case 73:
                        //        {
                        //            string s1 = $"{r[pos + 2].ToString("x2")}年{r[pos + 1].ToString("x2")}月{r[pos].ToString("x2")}日"; pos += 3;
                        //            string s2 = $"{BitConverter.ToUInt16(new byte[2] { r[pos], r[pos + 1] }, 0)}分钟"; pos += 2;
                        //            string s3 = $"{r[pos + 1].ToString("x2")}.{r[pos].ToString("x2")}℃"; pos += 2;
                        //            string s4 = $"{r[pos + 2].ToString("x2")}日{r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 3;
                        //            string s5 = $"{r[pos + 1].ToString("x2")}.{r[pos].ToString("x2")}℃"; pos += 2;
                        //            string s6 = $"{r[pos + 2].ToString("x2")}日{r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 3;

                        //            string t = $"{r[pos + 3].ToString("x2")}:{r[pos + 2].ToString("x2")}:{r[pos + 1].ToString("x2")}";
                        //            s = $"{t}\r\n时标：{s1}\r\n温度越上限累计时间：{s2}\r\n温度最高值：{s3}\r\n最高值发生时间：{s4}\r\n温度最低值：{s5}\r\n最低值发生时间：{s6}";

                        //            break;
                        //        }
                        //    case 74:
                        //        {
                        //            string s1 = $"{r[pos + 1].ToString("x2")}年{r[pos].ToString("x2")}月"; pos += 2;
                        //            string s2 = $"{BitConverter.ToUInt16(new byte[2] { r[pos], r[pos + 1] }, 0)}分钟"; pos += 2;
                        //            string s3 = $"{r[pos + 1].ToString("x2")}.{r[pos].ToString("x2")}℃"; pos += 2;
                        //            string s4 = $"{r[pos + 2].ToString("x2")}日{r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 3;
                        //            string s5 = $"{r[pos + 1].ToString("x2")}.{r[pos].ToString("x2")}℃"; pos += 2;
                        //            string s6 = $"{r[pos + 2].ToString("x2")}日{r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 3;

                        //            string t = $"{r[pos + 3].ToString("x2")}:{r[pos + 2].ToString("x2")}:{r[pos + 1].ToString("x2")}";
                        //            s = $"{t}\r\n时标：{s1}\r\n温度越上限累计时间：{s2}\r\n温度最高值：{s3}\r\n最高值发生时间：{s4}\r\n温度最低值：{s5}\r\n最低值发生时间：{s6}";

                        //            break;
                        //        }
                        //    case 75:
                        //        {
                        //            string s1 = $"{r[pos + 2].ToString("x2")}年{r[pos + 1].ToString("x2")}月{r[pos].ToString("x2")}日"; pos += 3;
                        //            string s2 = $"{BitConverter.ToUInt16(new byte[2] { r[pos], r[pos + 1] }, 0)}分钟"; pos += 2;
                        //            string s3 = $"{r[pos + 1].ToString("x2")}.{r[pos].ToString("x2")}%"; pos += 2;
                        //            string s4 = $"{r[pos + 2].ToString("x2")}日{r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 3;
                        //            string s5 = $"{r[pos + 1].ToString("x2")}.{r[pos].ToString("x2")}%"; pos += 2;
                        //            string s6 = $"{r[pos + 2].ToString("x2")}日{r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 3;

                        //            string t = $"{r[pos + 3].ToString("x2")}:{r[pos + 2].ToString("x2")}:{r[pos + 1].ToString("x2")}";
                        //            s = $"{t}\r\n时标：{s1}\r\n湿度越上限累计时间：{s2}\r\n湿度最高值：{s3}\r\n最高值发生时间：{s4}\r\n湿度最低值：{s5}\r\n最低值发生时间：{s6}";
                        //            break;
                        //        }
                        //    case 76:
                        //        {
                        //            string s1 = $"{r[pos + 1].ToString("x2")}年{r[pos].ToString("x2")}月"; pos += 2;
                        //            string s2 = $"{BitConverter.ToUInt16(new byte[2] { r[pos], r[pos + 1] }, 0)}分钟"; pos += 2;
                        //            string s3 = $"{r[pos + 1].ToString("x2")}.{r[pos].ToString("x2")}%"; pos += 2;
                        //            string s4 = $"{r[pos + 2].ToString("x2")}日{r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 3;
                        //            string s5 = $"{r[pos + 1].ToString("x2")}.{r[pos].ToString("x2")}%"; pos += 2;
                        //            string s6 = $"{r[pos + 2].ToString("x2")}日{r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 3;

                        //            string t = $"{r[pos + 3].ToString("x2")}:{r[pos + 2].ToString("x2")}:{r[pos + 1].ToString("x2")}";
                        //            s = $"{t}\r\n时标：{s1}\r\n湿度越上限累计时间：{s2}\r\n湿度最高值：{s3}\r\n最高值发生时间：{s4}\r\n湿度最低值：{s5}\r\n最低值发生时间：{s6}";

                        //            break;
                        //        }
                        //    case 81:
                        //        {
                        //            string s1 = $"{r[pos + 2].ToString("x2")}年{r[pos + 1].ToString("x2")}月{r[pos].ToString("x2")}日"; pos += 3;

                        //            double d1 = ToolFunc.GetAppendB29(r, pos); pos += 3;
                        //            double d2 = ToolFunc.GetAppendB29(r, pos); pos += 3;
                        //            double d3 = ToolFunc.GetAppendB29(r, pos); pos += 3;
                        //            double d4 = ToolFunc.GetAppendB29(r, pos); pos += 3;

                        //            string t = $"{r[pos + 3].ToString("x2")}:{r[pos + 2].ToString("x2")}:{r[pos + 1].ToString("x2")}";
                        //            s = $"{t}\r\n时标：{s1}\rAC220V电能：{d1}kWh\r\nAC24V电能：{d2}kWh\r\nDC12V电能：{d3}kWh\r\n单仓总电能：{d4}kWh";

                        //            break;
                        //        }
                        //    case 82:
                        //        {
                        //            string s1 = $"{r[pos + 1].ToString("x2")}月{r[pos].ToString("x2")}日"; pos += 2;

                        //            double d1 = ToolFunc.GetAppendB29(r, pos); pos += 3;
                        //            double d2 = ToolFunc.GetAppendB29(r, pos); pos += 3;
                        //            double d3 = ToolFunc.GetAppendB29(r, pos); pos += 3;
                        //            double d4 = ToolFunc.GetAppendB29(r, pos); pos += 3;

                        //            string t = $"{r[pos + 3].ToString("x2")}:{r[pos + 2].ToString("x2")}:{r[pos + 1].ToString("x2")}";
                        //            s = $"{t}\r\n时标：{s1}\rAC220V电能：{d1}kWh\r\nAC24V电能：{d2}kWh\r\nDC12V电能：{d3}kWh\r\n单仓总电能：{d4}kWh";

                        //            break;
                        //        }
                        //    case 83:
                        //        {
                        //            string s1 = $"{r[pos + 2].ToString("x2")}年{r[pos + 1].ToString("x2")}月{r[pos].ToString("x2")}日"; pos += 3;
                        //            string s2 = $"{BitConverter.ToUInt16(new byte[2] { r[pos], r[pos + 1] }, 0)}分钟"; pos += 2;
                        //            string s3 = $"{BitConverter.ToUInt16(new byte[2] { r[pos], r[pos + 1] }, 0)}分钟"; pos += 2;
                        //            string s4 = $"{BitConverter.ToUInt16(new byte[2] { r[pos], r[pos + 1] }, 0)}分钟"; pos += 2;
                        //            string s5 = $"{BitConverter.ToUInt16(new byte[2] { r[pos], r[pos + 1] }, 0)}分钟"; pos += 2;
                        //            string s6 = $"{BitConverter.ToUInt16(new byte[2] { r[pos], r[pos + 1] }, 0)}分钟"; pos += 2;
                        //            string s7 = $"{BitConverter.ToUInt16(new byte[2] { r[pos], r[pos + 1] }, 0)}分钟"; pos += 2;

                        //            string s8 = $"{r[pos + 1].ToString("x2")}.{r[pos].ToString("x2")}V"; pos += 2;
                        //            string s9 = $"{r[pos + 2].ToString("x2")}日{r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 3;
                        //            string s10 = $"{r[pos + 1].ToString("x2")}.{r[pos].ToString("x2")}V"; pos += 2;
                        //            string s11 = $"{r[pos + 2].ToString("x2")}日{r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 3;

                        //            string s12 = $"{r[pos + 1].ToString("x2")}.{r[pos].ToString("x2")}V"; pos += 2;
                        //            string s13 = $"{r[pos + 2].ToString("x2")}日{r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 3;
                        //            string s14 = $"{r[pos + 1].ToString("x2")}.{r[pos].ToString("x2")}V"; pos += 2;
                        //            string s15 = $"{r[pos + 2].ToString("x2")}日{r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 3;

                        //            string s16 = $"{r[pos + 1].ToString("x2")}.{r[pos].ToString("x2")}V"; pos += 2;
                        //            string s17 = $"{r[pos + 2].ToString("x2")}日{r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 3;
                        //            string s18 = $"{r[pos + 1].ToString("x2")}.{r[pos].ToString("x2")}V"; pos += 2;
                        //            string s19 = $"{r[pos + 2].ToString("x2")}日{r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 3;

                        //            string t = $"{r[pos + 3].ToString("x2")}:{r[pos + 2].ToString("x2")}:{r[pos + 1].ToString("x2")}";
                        //            s = $"{t}\r\n时标：{s1}\rAC220V越下限累计时间：{s2}\r\nAC220V越上限累计时间：{s3}\r\nAC24V越上限累计时间：{s4}" +
                        //                $"\r\nAC24V越下限累计时间：{s5}\r\nDC12V越上限累计时间：{s6}\r\nDC12V越下限累计时间：{s7}\r\nAC220V电压最大值：{s8}" +
                        //                $"\r\nAC220V电压最大值发生时间：{s9}\r\nAC220V电压最小值：{s10}\r\nAC220V电压最小值发生时间：{s11}" +
                        //                $"\r\nAC24V电压最大值：{s12}\r\nAC24V电压最大值发生时间：{s13}\r\nAC24V电压最小值：{s14}\r\nAC24V电压最小值发生时间：{s15}" +
                        //                $"\r\nDC12V电压最大值：{s16}\r\nDC12V电压最大值发生时间：{s17}\r\nDC12V电压最小值：{s18}\r\nDC12V电压最小值发生时间：{s19}";

                        //            break;
                        //        }
                        //    case 84:
                        //        {
                        //            string s1 = $"{r[pos + 1].ToString("x2")}年{r[pos].ToString("x2")}月"; pos += 2;
                        //            string s2 = $"{BitConverter.ToUInt16(new byte[2] { r[pos], r[pos + 1] }, 0)}分钟"; pos += 2;
                        //            string s3 = $"{BitConverter.ToUInt16(new byte[2] { r[pos], r[pos + 1] }, 0)}分钟"; pos += 2;
                        //            string s4 = $"{BitConverter.ToUInt16(new byte[2] { r[pos], r[pos + 1] }, 0)}分钟"; pos += 2;
                        //            string s5 = $"{BitConverter.ToUInt16(new byte[2] { r[pos], r[pos + 1] }, 0)}分钟"; pos += 2;
                        //            string s6 = $"{BitConverter.ToUInt16(new byte[2] { r[pos], r[pos + 1] }, 0)}分钟"; pos += 2;
                        //            string s7 = $"{BitConverter.ToUInt16(new byte[2] { r[pos], r[pos + 1] }, 0)}分钟"; pos += 2;

                        //            string s8 = $"{r[pos + 1].ToString("x2")}.{r[pos].ToString("x2")}V"; pos += 2;
                        //            string s9 = $"{r[pos + 2].ToString("x2")}日{r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 3;
                        //            string s10 = $"{r[pos + 1].ToString("x2")}.{r[pos].ToString("x2")}V"; pos += 2;
                        //            string s11 = $"{r[pos + 2].ToString("x2")}日{r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 3;

                        //            string s12 = $"{r[pos + 1].ToString("x2")}.{r[pos].ToString("x2")}V"; pos += 2;
                        //            string s13 = $"{r[pos + 2].ToString("x2")}日{r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 3;
                        //            string s14 = $"{r[pos + 1].ToString("x2")}.{r[pos].ToString("x2")}V"; pos += 2;
                        //            string s15 = $"{r[pos + 2].ToString("x2")}日{r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 3;

                        //            string s16 = $"{r[pos + 1].ToString("x2")}.{r[pos].ToString("x2")}V"; pos += 2;
                        //            string s17 = $"{r[pos + 2].ToString("x2")}日{r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 3;
                        //            string s18 = $"{r[pos + 1].ToString("x2")}.{r[pos].ToString("x2")}V"; pos += 2;
                        //            string s19 = $"{r[pos + 2].ToString("x2")}日{r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 3;

                        //            string t = $"{r[pos + 3].ToString("x2")}:{r[pos + 2].ToString("x2")}:{r[pos + 1].ToString("x2")}";
                        //            s = $"{t}\r\n时标：{s1}\rAC220V越下限累计时间：{s2}\r\nAC220V越上限累计时间：{s3}\r\nAC24V越上限累计时间：{s4}" +
                        //                $"\r\nAC24V越下限累计时间：{s5}\r\nDC12V越上限累计时间：{s6}\r\nDC12V越下限累计时间：{s7}\r\nAC220V电压最大值：{s8}" +
                        //                $"\r\nAC220V电压最大值发生时间：{s9}\r\nAC220V电压最小值：{s10}\r\nAC220V电压最小值发生时间：{s11}" +
                        //                $"\r\nAC24V电压最大值：{s12}\r\nAC24V电压最大值发生时间：{s13}\r\nAC24V电压最小值：{s14}\r\nAC24V电压最小值发生时间：{s15}" +
                        //                $"\r\nDC12V电压最大值：{s16}\r\nDC12V电压最大值发生时间：{s17}\r\nDC12V电压最小值：{s18}\r\nDC12V电压最小值发生时间：{s19}";

                        //            break;
                        //        }
                        //    case 85:
                        //        {
                        //            string s1 = $"{r[pos + 2].ToString("x2")}年{r[pos + 1].ToString("x2")}月{r[pos].ToString("x2")}日"; pos += 3;
                        //            double num1 = ToolFunc.GetAppendB29(r, pos); pos += 3;

                        //            string t = $"{r[pos + 3].ToString("x2")}:{r[pos + 2].ToString("x2")}:{r[pos + 1].ToString("x2")}";
                        //            s = $"{t}\r\n时标：{s1}\r\n电能量：{num1}kWh";

                        //            break;
                        //        }
                        //    case 86:
                        //        {
                        //            string s1 = $"{r[pos + 1].ToString("x2")}年{r[pos].ToString("x2")}月"; pos += 2;
                        //            double num1 = ToolFunc.GetAppendB29(r, pos); pos += 3;

                        //            string t = $"{r[pos + 3].ToString("x2")}:{r[pos + 2].ToString("x2")}:{r[pos + 1].ToString("x2")}";
                        //            s = $"{t}\r\n时标：{s1}\r\n电能量：{num1}kWh";
                        //            break;
                        //        }
                        //    case 87:
                        //        {
                        //            string s1 = $"{r[pos + 2].ToString("x2")}年{r[pos + 1].ToString("x2")}月{r[pos].ToString("x2")}日"; pos += 3;
                        //            string s2 = $"{BitConverter.ToUInt16(new byte[2] { r[pos], r[pos + 1] }, 0)}分钟"; pos += 2;
                        //            string s3 = $"{BitConverter.ToUInt16(new byte[2] { r[pos], r[pos + 1] }, 0)}分钟"; pos += 2;
                        //            string s4 = $"{r[pos + 1].ToString("x2")}.{r[pos].ToString("x2")}V"; pos += 2;
                        //            string s5 = $"{r[pos + 2].ToString("x2")}日{r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 3;
                        //            string s6 = $"{r[pos + 1].ToString("x2")}.{r[pos].ToString("x2")}V"; pos += 2;
                        //            string s7 = $"{r[pos + 2].ToString("x2")}日{r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 3;


                        //            string t = $"{r[pos + 3].ToString("x2")}:{r[pos + 2].ToString("x2")}:{r[pos + 1].ToString("x2")}";
                        //            s = $"{t}\r\n时标：{s1}\r\n越上限累计时间：{s2}\r\n越下限累计时间：{s3}\r\n电压最大值：{s4}\r\n最大值发生时间：{s5}\r\n电压最小值：{s6}\r\n最小值发生时间：{s7}";

                        //            break;
                        //        }
                        //    case 88:
                        //        {
                        //            string s1 = $"{r[pos + 1].ToString("x2")}年{r[pos].ToString("x2")}月"; pos += 2;
                        //            string s2 = $"{BitConverter.ToUInt16(new byte[2] { r[pos], r[pos + 1] }, 0)}分钟"; pos += 2;
                        //            string s3 = $"{BitConverter.ToUInt16(new byte[2] { r[pos], r[pos + 1] }, 0)}分钟"; pos += 2;
                        //            string s4 = $"{r[pos + 1].ToString("x2")}.{r[pos].ToString("x2")}V"; pos += 2;
                        //            string s5 = $"{r[pos + 2].ToString("x2")}日{r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 3;
                        //            string s6 = $"{r[pos + 1].ToString("x2")}.{r[pos].ToString("x2")}V"; pos += 2;
                        //            string s7 = $"{r[pos + 2].ToString("x2")}日{r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 3;


                        //            string t = $"{r[pos + 3].ToString("x2")}:{r[pos + 2].ToString("x2")}:{r[pos + 1].ToString("x2")}";
                        //            s = $"{t}\r\n时标：{s1}\r\n越上限累计时间：{s2}\r\n越下限累计时间：{s3}\r\n电压最大值：{s4}\r\n最大值发生时间：{s5}\r\n电压最小值：{s6}\r\n最小值发生时间：{s7}";

                        //            break;
                        //        }
                        //    case 89:
                        //        {
                        //            string s1 = $"{r[pos + 4].ToString("x2")}-{r[pos + 3].ToString("x2")}-{r[pos + 2].ToString("x2")} {r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 5;
                        //            string s2 = "不冻结";
                        //            int duraTime = 0;
                        //            switch (r[pos++])
                        //            {
                        //                case 0x01: s2 = "15min"; duraTime = 15; break;
                        //                case 0x02: s2 = "30min"; duraTime = 30; break;
                        //                case 0x03: s2 = "60min"; duraTime = 60; break;
                        //                case 0xFE: s2 = "5min"; duraTime = 5; break;
                        //                case 0xFF: s2 = "1min"; duraTime = 1; break;
                        //            }
                        //            //int duraTime = int.Parse(s2);
                        //            int s3 = r[pos++];
                        //            var li = new System.Collections.Generic.List<string>();
                        //            DateTime stime = DateTime.Parse(s1);
                        //            for (int i = 0; i < s3; ++i)
                        //            {
                        //                stime = stime.AddMinutes(duraTime);
                        //                li.Add($"{stime.Day:D2}日{stime.Hour:D2}:{stime.Minute:D2} | {r[pos + 1].ToString("x2")}.{r[pos].ToString("x2")}");
                        //                pos += 3;
                        //            }
                        //            string s4 = "数据：\r\n";
                        //            foreach (var d in li)
                        //            {
                        //                s4 += $"{d}℃\r\n";
                        //            }

                        //            string t = $"{r[pos + 3].ToString("x2")}:{r[pos + 2].ToString("x2")}:{r[pos + 1].ToString("x2")}";
                        //            s = $"{t}\r\n时标：{s1}\r\n数据密度：{s2}\r\n数据点数：{s3}\r\n{s4}";

                        //            break;
                        //        }
                        //    case 90:
                        //        {
                        //            string s1 = $"{r[pos + 4].ToString("x2")}-{r[pos + 3].ToString("x2")}-{r[pos + 2].ToString("x2")} {r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 5;
                        //            string s2 = "不冻结";
                        //            int duraTime = 0;
                        //            switch (r[pos++])
                        //            {
                        //                case 0x01: s2 = "15min"; duraTime = 15; break;
                        //                case 0x02: s2 = "30min"; duraTime = 30; break;
                        //                case 0x03: s2 = "60min"; duraTime = 60; break;
                        //                case 0xFE: s2 = "5min"; duraTime = 5; break;
                        //                case 0xFF: s2 = "1min"; duraTime = 1; break;
                        //            }
                        //            //int duraTime = int.Parse(s2);
                        //            int s3 = r[pos++];
                        //            var li = new System.Collections.Generic.List<string>();
                        //            DateTime stime = DateTime.Parse(s1);
                        //            for (int i = 0; i < s3; ++i)
                        //            {
                        //                stime = stime.AddMinutes(duraTime);
                        //                li.Add($"{stime.Day:D2}日{stime.Hour:D2}:{stime.Minute:D2} | {r[pos + 1].ToString("x2")}.{r[pos].ToString("x2")}");
                        //                pos += 3;
                        //            }
                        //            string s4 = "数据：\r\n";
                        //            foreach (var d in li)
                        //            {
                        //                s4 += $"{d}%\r\n";
                        //            }

                        //            string t = $"{r[pos + 3].ToString("x2")}:{r[pos + 2].ToString("x2")}:{r[pos + 1].ToString("x2")}";
                        //            s = $"{t}\r\n时标：{s1}\r\n数据密度：{s2}\r\n数据点数：{s3}\r\n{s4}";

                        //            break;
                        //        }
                        //    case 97:
                        //    case 98:
                        //        {
                        //            string s1 = $"{r[pos + 4].ToString("x2")}-{r[pos + 3].ToString("x2")}-{r[pos + 2].ToString("x2")} {r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 5;
                        //            string s2 = "不冻结";
                        //            int duraTime = 0;
                        //            switch (r[pos++])
                        //            {
                        //                case 0x01: s2 = "15min"; duraTime = 15; break;
                        //                case 0x02: s2 = "30min"; duraTime = 30; break;
                        //                case 0x03: s2 = "60min"; duraTime = 60; break;
                        //                case 0xFE: s2 = "5min"; duraTime = 5; break;
                        //                case 0xFF: s2 = "1min"; duraTime = 1; break;
                        //            }
                        //            //int duraTime = int.Parse(s2);
                        //            int s3 = r[pos++];
                        //            var liAC220V = new System.Collections.Generic.List<string>();
                        //            var liAC24V = new System.Collections.Generic.List<string>();
                        //            var liDC12V = new System.Collections.Generic.List<string>();
                        //            DateTime stime = DateTime.Parse(s1);
                        //            string dt = "";
                        //            for (int i = 0; i < s3; ++i)
                        //            {
                        //                stime = stime.AddMinutes(duraTime);
                        //                dt = $"{stime.Day:D2}日{stime.Hour:D2}:{stime.Minute:D2}";

                        //                liAC220V.Add($"{dt} | {ToolFunc.GetAppendB25(r, pos)}A"); pos += 3;
                        //                liAC24V.Add($"{dt} | {ToolFunc.GetAppendB25(r, pos)}A"); pos += 3;
                        //                liDC12V.Add($"{dt} | {ToolFunc.GetAppendB25(r, pos)}A"); pos += 3;
                        //            }
                        //            string s4 = "数据（AC220V）：\r\n";
                        //            for (int i = 0; i < s3; ++i)
                        //            {
                        //                s4 += $"{liAC220V[i]}\r\n";
                        //            }
                        //            s4 += "数据（AC24V）：\r\n";
                        //            for (int i = 0; i < s3; ++i)
                        //            {
                        //                s4 += $"{liAC24V[i]}\r\n";
                        //            }
                        //            s4 += "数据（DC12V）：\r\n";
                        //            for (int i = 0; i < s3; ++i)
                        //            {
                        //                s4 += $"{liDC12V[i]}\r\n";
                        //            }

                        //            string t = $"{r[pos + 3].ToString("x2")}:{r[pos + 2].ToString("x2")}:{r[pos + 1].ToString("x2")}";
                        //            s = $"{t}\r\n时标：{s1}\r\n数据密度：{s2}\r\n数据点数：{s3}\r\n{s4}";
                        //            break;
                        //        }
                        //    case 99:
                        //        {
                        //            string s1 = $"{r[pos + 4].ToString("x2")}-{r[pos + 3].ToString("x2")}-{r[pos + 2].ToString("x2")} {r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 5;
                        //            string s2 = "不冻结";
                        //            int duraTime = 0;
                        //            switch (r[pos++])
                        //            {
                        //                case 0x01: s2 = "15min"; duraTime = 15; break;
                        //                case 0x02: s2 = "30min"; duraTime = 30; break;
                        //                case 0x03: s2 = "60min"; duraTime = 60; break;
                        //                case 0xFE: s2 = "5min"; duraTime = 5; break;
                        //                case 0xFF: s2 = "1min"; duraTime = 1; break;
                        //            }
                        //            //int duraTime = int.Parse(s2);
                        //            int s3 = r[pos++];
                        //            var liAC220V = new System.Collections.Generic.List<string>();
                        //            var liAC24V = new System.Collections.Generic.List<string>();
                        //            var liDC12V = new System.Collections.Generic.List<string>();
                        //            var liTotal = new System.Collections.Generic.List<string>();
                        //            DateTime stime = DateTime.Parse(s1);
                        //            string dt = "";
                        //            for (int i = 0; i < s3; ++i)
                        //            {
                        //                stime = stime.AddMinutes(duraTime);
                        //                dt = $"{stime.Day:D2}日{stime.Hour:D2}:{stime.Minute:D2}";

                        //                liAC220V.Add($"{dt} | {ToolFunc.GetAppendB2(r, pos)}W"); pos += 2;
                        //                liAC24V.Add($"{dt} | {ToolFunc.GetAppendB2(r, pos)}W"); pos += 2;
                        //                liDC12V.Add($"{dt} | {ToolFunc.GetAppendB2(r, pos)}W"); pos += 2;
                        //                liTotal.Add($"{dt} | {ToolFunc.GetAppendB2(r, pos)}W"); pos += 2;
                        //            }
                        //            string s4 = "数据（AC220V）：\r\n";
                        //            for (int i = 0; i < s3; ++i)
                        //            {
                        //                s4 += $"{liAC220V[i]}\r\n";
                        //            }
                        //            s4 += "数据（AC24V）：\r\n";
                        //            for (int i = 0; i < s3; ++i)
                        //            {
                        //                s4 += $"{liAC24V[i]}\r\n";
                        //            }
                        //            s4 += "数据（DC12V）：\r\n";
                        //            for (int i = 0; i < s3; ++i)
                        //            {
                        //                s4 += $"{liDC12V[i]}\r\n";
                        //            }
                        //            s4 += "数据（总功率）：\r\n";
                        //            for (int i = 0; i < s3; ++i)
                        //            {
                        //                s4 += $"{liTotal[i]}\r\n";
                        //            }

                        //            string t = $"{r[pos + 3].ToString("x2")}:{r[pos + 2].ToString("x2")}:{r[pos + 1].ToString("x2")}";
                        //            s = $"{t}\r\n时标：{s1}\r\n数据密度：{s2}\r\n数据点数：{s3}\r\n{s4}";

                        //            break;
                        //        }
                        //    case 100:
                        //        {
                        //            string s1 = $"{r[pos + 4].ToString("x2")}-{r[pos + 3].ToString("x2")}-{r[pos + 2].ToString("x2")} {r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 5;
                        //            string s2 = "不冻结";
                        //            int duraTime = 0;
                        //            switch (r[pos++])
                        //            {
                        //                case 0x01: s2 = "15min"; duraTime = 15; break;
                        //                case 0x02: s2 = "30min"; duraTime = 30; break;
                        //                case 0x03: s2 = "60min"; duraTime = 60; break;
                        //                case 0xFE: s2 = "5min"; duraTime = 5; break;
                        //                case 0xFF: s2 = "1min"; duraTime = 1; break;
                        //            }
                        //            //int duraTime = int.Parse(s2);
                        //            int s3 = r[pos++];
                        //            var li = new System.Collections.Generic.List<string>();
                        //            DateTime stime = DateTime.Parse(s1);
                        //            for (int i = 0; i < s3; ++i)
                        //            {
                        //                stime = stime.AddMinutes(duraTime);
                        //                li.Add($"{stime.Day:D2}日{stime.Hour:D2}:{stime.Minute:D2} | {r[pos + 1].ToString("x2")}.{r[pos].ToString("x2")}V");
                        //                pos += 2;
                        //            }
                        //            string s4 = "数据：\r\n";
                        //            foreach (var d in li)
                        //            {
                        //                s4 += $"{d}\r\n";
                        //            }

                        //            string t = $"{r[pos + 3].ToString("x2")}:{r[pos + 2].ToString("x2")}:{r[pos + 1].ToString("x2")}";
                        //            s = $"{t}\r\n时标：{s1}\r\n数据密度：{s2}\r\n数据点数：{s3}\r\n{s4}";
                        //            break;
                        //        }
                        //    case 101:
                        //        {
                        //            string s1 = $"{r[pos + 4].ToString("x2")}-{r[pos + 3].ToString("x2")}-{r[pos + 2].ToString("x2")} {r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 5;
                        //            string s2 = "不冻结";
                        //            int duraTime = 0;
                        //            switch (r[pos++])
                        //            {
                        //                case 0x01: s2 = "15min"; duraTime = 15; break;
                        //                case 0x02: s2 = "30min"; duraTime = 30; break;
                        //                case 0x03: s2 = "60min"; duraTime = 60; break;
                        //                case 0xFE: s2 = "5min"; duraTime = 5; break;
                        //                case 0xFF: s2 = "1min"; duraTime = 1; break;
                        //            }
                        //            //int duraTime = int.Parse(s2);
                        //            int s3 = r[pos++];
                        //            var li = new System.Collections.Generic.List<string>();
                        //            DateTime stime = DateTime.Parse(s1);
                        //            for (int i = 0; i < s3; ++i)
                        //            {
                        //                stime = stime.AddMinutes(duraTime);
                        //                li.Add($"{stime.Day:D2}日{stime.Hour:D2}:{stime.Minute:D2} | {ToolFunc.GetAppendB25(r, pos)}A");
                        //                pos += 3;
                        //            }
                        //            string s4 = "数据：\r\n";
                        //            foreach (var d in li)
                        //            {
                        //                s4 += $"{d}\r\n";
                        //            }

                        //            string t = $"{r[pos + 3].ToString("x2")}:{r[pos + 2].ToString("x2")}:{r[pos + 1].ToString("x2")}";
                        //            s = $"{t}\r\n时标：{s1}\r\n数据密度：{s2}\r\n数据点数：{s3}\r\n{s4}";
                        //            break;
                        //        }
                        //    case 102:
                        //        {
                        //            string s1 = $"{r[pos + 4].ToString("x2")}-{r[pos + 3].ToString("x2")}-{r[pos + 2].ToString("x2")} {r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 5;
                        //            string s2 = "不冻结";
                        //            int duraTime = 0;
                        //            switch (r[pos++])
                        //            {
                        //                case 0x01: s2 = "15min"; duraTime = 15; break;
                        //                case 0x02: s2 = "30min"; duraTime = 30; break;
                        //                case 0x03: s2 = "60min"; duraTime = 60; break;
                        //                case 0xFE: s2 = "5min"; duraTime = 5; break;
                        //                case 0xFF: s2 = "1min"; duraTime = 1; break;
                        //            }
                        //            //int duraTime = int.Parse(s2);
                        //            int s3 = r[pos++];
                        //            var li = new System.Collections.Generic.List<string>();
                        //            DateTime stime = DateTime.Parse(s1);
                        //            for (int i = 0; i < s3; ++i)
                        //            {
                        //                stime = stime.AddMinutes(duraTime);
                        //                li.Add($"{stime.Day:D2}日{stime.Hour:D2}:{stime.Minute:D2} | {ToolFunc.GetAppendB2(r, pos)}W");
                        //                pos += 2;
                        //            }
                        //            string s4 = "数据：\r\n";
                        //            foreach (var d in li)
                        //            {
                        //                s4 += $"{d}\r\n";
                        //            }

                        //            string t = $"{r[pos + 3].ToString("x2")}:{r[pos + 2].ToString("x2")}:{r[pos + 1].ToString("x2")}";
                        //            s = $"{t}\r\n时标：{s1}\r\n数据密度：{s2}\r\n数据点数：{s3}\r\n{s4}";
                        //            break;
                        //        }
                        //    case 103:
                        //        {
                        //            string s1 = $"{r[pos + 4].ToString("x2")}-{r[pos + 3].ToString("x2")}-{r[pos + 2].ToString("x2")} {r[pos + 1].ToString("x2")}:{r[pos].ToString("x2")}"; pos += 5;
                        //            string s2 = "不冻结";
                        //            int duraTime = 0;
                        //            switch (r[pos++])
                        //            {
                        //                case 0x01: s2 = "15min"; duraTime = 15; break;
                        //                case 0x02: s2 = "30min"; duraTime = 30; break;
                        //                case 0x03: s2 = "60min"; duraTime = 60; break;
                        //                case 0xFE: s2 = "5min"; duraTime = 5; break;
                        //                case 0xFF: s2 = "1min"; duraTime = 1; break;
                        //            }
                        //            //int duraTime = int.Parse(s2);
                        //            int s3 = r[pos++];
                        //            var li = new System.Collections.Generic.List<string>();
                        //            DateTime stime = DateTime.Parse(s1);
                        //            for (int i = 0; i < s3; ++i)
                        //            {
                        //                stime = stime.AddMinutes(duraTime);
                        //                li.Add($"{stime.Day:D2}日{stime.Hour:D2}:{stime.Minute:D2} | {ToolFunc.GetAppendB2(r, pos)}W");
                        //                pos += 2;
                        //            }
                        //            string s4 = "数据：\r\n";
                        //            foreach (var d in li)
                        //            {
                        //                s4 += $"{d}\r\n";
                        //            }

                        //            string t = $"{r[pos + 3].ToString("x2")}:{r[pos + 2].ToString("x2")}:{r[pos + 1].ToString("x2")}";
                        //            s = $"{t}\r\n时标：{s1}\r\n数据密度：{s2}\r\n数据点数：{s3}\r\n{s4}";

                        //            break;
                        //        }
                        //    default:
                        //        break;
                        //}
                        break;
                    }
                case Comm.Afn0E:
                    {
                        //switch (Fn)
                        //{
                        //    case 1:
                        //        {
                        //            _0EAwsData _0EAws = null;
                        //            try
                        //            {
                        //                _0EAws = (_0EAwsData)ExtractPackage.StructExtract(r);
                        //            }
                        //            catch (Exception)
                        //            {
                        //                _0EAws = null;
                        //            }
                        //            if (_0EAws == null)
                        //            {
                        //                s = "读取错误";
                        //            }
                        //            else
                        //            {
                        //                s += $"当前重要事件数:{_0EAws.EC1}\r\n当前一般事件数:{_0EAws.EC2}\r\n起始指针:{_0EAws.Pm}\r\n结束指针:{_0EAws.Pn}\r\n";
                        //                if (_0EAws.E1.Count > 0)
                        //                    foreach (_0EAwsE1 _0EAwsE1 in _0EAws.E1)
                        //                        s += $"=====>ACU失电记录\r\n发生时间:{_0EAwsE1.E1_Datetime}\r\n记录类型:{_0EAwsE1.E1_Remark}\r\n";
                        //                if (_0EAws.E51.Count > 0)
                        //                    foreach (_0EAwsE51 _0EAwsE51 in _0EAws.E51)
                        //                        s += $"=====>升级成功事件记录\r\n发生时间:{_0EAwsE51.E51_Datetime}\r\n记录类型:{_0EAwsE51.E51_Remark}\r\n当前版本号:{_0EAwsE51.E51_Version}\r\n";
                        //                if (_0EAws.E52.Count > 0)
                        //                    foreach (_0EAwsE52 _0EAwsE52 in _0EAws.E52)
                        //                        s += $"=====>温湿度越限记录\r\n发生时间:{_0EAwsE52.E52_Datetime}\r\n记录类型:{_0EAwsE52.E52_Remark}\r\n" +
                        //                        $"事件类别:{_0EAwsE52.E52_EventType}\r\n温度/湿度传感器号:{_0EAwsE52.E52_Pn}\r\n温度℃/湿度%:{_0EAwsE52.E52_EventNum}\r\n";
                        //                if (_0EAws.E53.Count > 0)
                        //                    foreach (_0EAwsE53 _0EAwsE53 in _0EAws.E53)
                        //                        s += $"=====>积水告警记录\r\n发生时间:{_0EAwsE53.E53_Datetime}\r\n记录类型:{_0EAwsE53.E53_Remark}\r\n";
                        //                if (_0EAws.E54.Count > 0)
                        //                    foreach (_0EAwsE54 _0EAwsE54 in _0EAws.E54)
                        //                        s += $"=====>箱门开启记录\r\n发生时间:{_0EAwsE54.E54_Datetime}\r\n记录类型:{_0EAwsE54.E54_Remark}\r\n" +
                        //                        $"箱门号:{_0EAwsE54.E54_DoorNum}\r\n";
                        //                if (_0EAws.E55.Count > 0)
                        //                    foreach (_0EAwsE55 _0EAwsE55 in _0EAws.E55)
                        //                        s += $"=====>风扇异常记录\r\n发生时间:{_0EAwsE55.E55_Datetime}\r\n记录类型:{_0EAwsE55.E55_Remark}\r\n" +
                        //                        $"风扇额定功率W:{_0EAwsE55.E55_FormalPower}\r\n发生时风扇功率W：{_0EAwsE55.E55_OcursePower}\r\n";
                        //                if (_0EAws.E56.Count > 0)
                        //                    foreach (_0EAwsE56 _0EAwsE56 in _0EAws.E56)
                        //                        s += $"=====>用户舱电压越限记录\r\n发生时间:{_0EAwsE56.E56_Datetime}\r\n记录类型:{_0EAwsE56.E56_Remark}\r\n" +
                        //                        $"用户舱号:{_0EAwsE56.E56_CageNum}\r\n分路标识:{_0EAwsE56.E56_LineNum}\r\n越限类型:{_0EAwsE56.E56_EventType}\r\n发生时的电压值V:{_0EAwsE56.E56_OcurseVoltage}";
                        //                if (_0EAws.E57.Count > 0)
                        //                    foreach (_0EAwsE57 _0EAwsE57 in _0EAws.E57)
                        //                        s += $"=====>用户舱总功率越上限记录\r\n发生时间:{_0EAwsE57.E57_Datetime}\r\n记录类型:{_0EAwsE57.E57_Remark}\r\n" +
                        //                        $"用户舱号:{_0EAwsE57.E57_CageNum}\r\n发生时的功率值W:{_0EAwsE57.E57_OcursePower}\r\n";
                        //                if (_0EAws.E58.Count > 0)
                        //                    foreach (_0EAwsE58 _0EAwsE58 in _0EAws.E58)
                        //                        s += $"=====>遥控跳闸记录\r\n发生时间:{_0EAwsE58.E58_Datetime}\r\n记录类型:{_0EAwsE58.E58_Remark}\r\n" +
                        //                        $"跳闸类别:{_0EAwsE58.E58_EventType}\r\n进线号/用户舱号:{_0EAwsE58.E58_CageOrLineNum}\r\n用户舱分路标识:{_0EAwsE58.E58_LineNum}\r\n";
                        //                if (_0EAws.E59.Count > 0)
                        //                    foreach (_0EAwsE59 _0EAwsE59 in _0EAws.E59)
                        //                        s += $"=====>电子锁开启记录\r\n发生时间:{_0EAwsE59.E59_Datetime}\r\n记录类型:{_0EAwsE59.E59_Remark}\r\n" +
                        //                        $"箱门号:{_0EAwsE59.E59_DoorNum}";
                        //            }
                        //            break;
                        //        }
                        //    case 2:
                        //        {
                        //            _0EAwsData _0EAws = null;
                        //            try
                        //            {
                        //                _0EAws = (_0EAwsData)ExtractPackage.StructExtract(r);
                        //            }
                        //            catch (Exception)
                        //            {
                        //                _0EAws = null;
                        //            }

                        //            if (_0EAws == null)
                        //            {
                        //                s = "读取错误";
                        //            }
                        //            else
                        //            {
                        //                s += $"当前重要事件数:{_0EAws.EC1}\r\n当前一般事件数:{_0EAws.EC2}\r\n起始指针:{_0EAws.Pm}\r\n结束指针:{_0EAws.Pn}\r\n";
                        //                if (_0EAws.E1.Count > 0)
                        //                    foreach (_0EAwsE1 _0EAwsE1 in _0EAws.E1)
                        //                        s += $"=====>ACU失电记录\r\n发生时间:{_0EAwsE1.E1_Datetime}\r\n记录类型:{_0EAwsE1.E1_Remark}\r\n";
                        //                if (_0EAws.E51.Count > 0)
                        //                    foreach (_0EAwsE51 _0EAwsE51 in _0EAws.E51)
                        //                        s += $"=====>升级成功事件记录\r\n发生时间:{_0EAwsE51.E51_Datetime}\r\n记录类型:{_0EAwsE51.E51_Remark}\r\n当前版本号:{_0EAwsE51.E51_Version}\r\n";
                        //                if (_0EAws.E52.Count > 0)
                        //                    foreach (_0EAwsE52 _0EAwsE52 in _0EAws.E52)
                        //                        s += $"=====>温湿度越限记录\r\n发生时间:{_0EAwsE52.E52_Datetime}\r\n记录类型:{_0EAwsE52.E52_Remark}\r\n" +
                        //                        $"事件类别:{_0EAwsE52.E52_EventType}\r\n温度/湿度传感器号:{_0EAwsE52.E52_Pn}\r\n温度℃/湿度%:{_0EAwsE52.E52_EventNum}\r\n";
                        //                if (_0EAws.E53.Count > 0)
                        //                    foreach (_0EAwsE53 _0EAwsE53 in _0EAws.E53)
                        //                        s += $"=====>积水告警记录\r\n发生时间:{_0EAwsE53.E53_Datetime}\r\n记录类型:{_0EAwsE53.E53_Remark}\r\n";
                        //                if (_0EAws.E54.Count > 0)
                        //                    foreach (_0EAwsE54 _0EAwsE54 in _0EAws.E54)
                        //                        s += $"=====>箱门开启记录\r\n发生时间:{_0EAwsE54.E54_Datetime}\r\n记录类型:{_0EAwsE54.E54_Remark}\r\n" +
                        //                        $"箱门号:{_0EAwsE54.E54_DoorNum}\r\n";
                        //                if (_0EAws.E55.Count > 0)
                        //                    foreach (_0EAwsE55 _0EAwsE55 in _0EAws.E55)
                        //                        s += $"=====>风扇异常记录\r\n发生时间:{_0EAwsE55.E55_Datetime}\r\n记录类型:{_0EAwsE55.E55_Remark}\r\n" +
                        //                        $"风扇额定功率W:{_0EAwsE55.E55_FormalPower}\r\n发生时风扇功率W：{_0EAwsE55.E55_OcursePower}\r\n";
                        //                if (_0EAws.E56.Count > 0)
                        //                    foreach (_0EAwsE56 _0EAwsE56 in _0EAws.E56)
                        //                        s += $"=====>用户舱电压越限记录\r\n发生时间:{_0EAwsE56.E56_Datetime}\r\n记录类型:{_0EAwsE56.E56_Remark}\r\n" +
                        //                        $"用户舱号:{_0EAwsE56.E56_CageNum}\r\n分路标识:{_0EAwsE56.E56_LineNum}\r\n越限类型:{_0EAwsE56.E56_EventType}\r\n发生时的电压值V:{_0EAwsE56.E56_OcurseVoltage}";
                        //                if (_0EAws.E57.Count > 0)
                        //                    foreach (_0EAwsE57 _0EAwsE57 in _0EAws.E57)
                        //                        s += $"=====>用户舱总功率越上限记录\r\n发生时间:{_0EAwsE57.E57_Datetime}\r\n记录类型:{_0EAwsE57.E57_Remark}\r\n" +
                        //                        $"用户舱号:{_0EAwsE57.E57_CageNum}\r\n发生时的功率值W:{_0EAwsE57.E57_OcursePower}\r\n";
                        //                if (_0EAws.E58.Count > 0)
                        //                    foreach (_0EAwsE58 _0EAwsE58 in _0EAws.E58)
                        //                        s += $"=====>遥控跳闸记录\r\n发生时间:{_0EAwsE58.E58_Datetime}\r\n记录类型:{_0EAwsE58.E58_Remark}\r\n" +
                        //                        $"跳闸类别:{_0EAwsE58.E58_EventType}\r\n进线号/用户舱号:{_0EAwsE58.E58_CageOrLineNum}\r\n用户舱分路标识:{_0EAwsE58.E58_LineNum}\r\n";
                        //                if (_0EAws.E59.Count > 0)
                        //                    foreach (_0EAwsE59 _0EAwsE59 in _0EAws.E59)
                        //                        s += $"=====>电子锁开启记录\r\n发生时间:{_0EAwsE59.E59_Datetime}\r\n记录类型:{_0EAwsE59.E59_Remark}\r\n" +
                        //                        $"箱门号:{_0EAwsE59.E59_DoorNum}";
                        //            }
                        //            break;
                        //        }
                        //    default:
                        //        break;
                        //}
                        break;
                    }
                case Comm.Afn10:
                    {
                        break;
                    }
                default:
                    break;
            }

            return s;
        }

        public static bool ValidPkg(byte[] r, ref string s)
        {
            bool bOk = true;
            int len = r.Length;
            if(len < (int)FramePosEnum.FramePostEnd) 
            {
                s += "帧长度不够！\r\n"; bOk = false; return bOk;
            }
            if (r[(int)FramePosEnum.Start1] != Comm.FrameStart 
                || r[(int)FramePosEnum.Start2] != Comm.FrameStart)
            {
                s += "起始字符不为 0x68！\r\n"; bOk = false;
            }
            if (r[len - 1] != Comm.FrameEnd)
            {
                s += "结束字符不为 0x16！\r\n"; bOk = false;
            }
            int bodyLen1 = r[(int)FramePosEnum.Len1] + r[(int)FramePosEnum.Len1 + 1] * 256;
            int bodyLen2 = r[(int)FramePosEnum.Len2] + r[(int)FramePosEnum.Len2 + 1] * 256;
            if (bodyLen1 != bodyLen2)
            {
                s += "2个长度L不同！\r\n"; bOk = false;
            }
            if (len != bodyLen1 + Comm.FrameFixLen)
            {
                s += "报文长度 != 数据长度L + 9\r\n"; bOk = false;
            }
            long crcnum = CRCHelper.GetCRC16(r, (int)FramePosEnum.Ctrl_TerminalId, r.Length - 3);
            byte crc1 = (byte)(crcnum % 256);
            byte crc2 = (byte)(crcnum / 256);
            if (crc1 != r[len - 3] && crc2 != r[len - 2])
            {
                s += "CRC校验错误！\r\n"; bOk = false;
            }
            byte afn = r[(int)FramePosEnum.User_Afn];
            if (afn != 0x00 && afn != 0x01 && afn != 0x02 
                && afn != 0x04 && afn != 0x05 && afn != 0x09 
                && afn != 0x0A && afn != 0x0C && afn != 0x0D 
                && afn != 0x0E && afn != 0x10)
            {
                s += "功能码错误！\r\n"; bOk = false;
            }
            return bOk;
        }

        #region 报文解析工具方法
        public static byte[] transPn(int pn)
        {
            if (pn == 0)
                return new byte[2];
            byte[] b = new byte[2];
            b[1] = (byte)((pn - 1) / 8 + 1);
            b[0] = (byte)(Math.Pow(2, (pn - 1) % 8));
            return b;
        }
        public static byte[] transFn(int fn)
        {
            byte[] b = new byte[2];
            b[1] = (byte)((fn - 1) / 8);
            b[0] = (byte)(Math.Pow(2, (fn - 1) % 8));
            return b;
        }
        public static int utransPn(byte[] pn)
        {
            int b = 1;
            if (pn[0] == 0 && pn[1] == 0)
                return 0;
            b += (pn[1] - 1) * 8;
            b += (int)Math.Log(pn[0], 2);
            return b;
        }
        public static int utransFn(byte[] fn)
        {
            int b = 1;
            b += fn[1] * 8;
            b += (int)Math.Log(fn[0], 2);
            return b;
        }

        public static byte getBaudRateByte(string baudRate)
        {
            switch (baudRate)
            {
                case "4800": return 0x01;
                case "9600": return 0x02;
                case "38400": return 0x04;
                case "115200": return 0x08;
                default: return 0x02;
            }
        }
        public static string getBaudRateStr(byte baudRate)
        {
            switch (baudRate)
            {
                case 0x01: return  "4800";
                case 0x02: return "9600";
                case 0x04: return "38400";
                case 0x08: return "115200";
                default: return "9600";
            }
        }

        #region 解析Tp时间
        public static string GetTPStr(byte[] raw)
        {
            string timestr = DateTime.Now.ToString("yyyy-MM-") +
                raw[3].ToString("x2") + " " +
                raw[2].ToString("x2") + ":" +
                raw[1].ToString("x2") + ":" +
                raw[0].ToString("x2");

            return timestr;
        } 
        #endregion

        #region GetSEQFlag 判断帧序列标志
        public static string GetSEQFlag(int seq)
        {
            int r = seq & 0x60;
            switch (r)
            {
                case 96: return "单帧";
                case 64: return "多帧，第一帧";
                case 32: return "多帧，结束帧";
                case 0: return "多帧，中间帧";
                default: return "未知帧标志";
            }
        }
        #endregion

        #endregion
    }
}
